#pragma once

#include <iostream>
#include <memory>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "err.hpp"
#include <functional>

namespace ns_server{
    const static uint16_t default_port = 8086;
    using func_t = std::function<std::string(std::string)>;

    class UdpServer{
        public:
            UdpServer(uint16_t port, func_t func) : port_(port), serverce_(func){
                std::cout << "server addr: " << port_ << std::endl;
            }

            void InitServer(){
                //1.创建socket接口，打开网络文件
                sock_ = socket(AF_INET, SOCK_DGRAM, 0);
                if(sock_ < 0){
                    std::cerr << "creat socket error: " << strerror(errno) << std::endl;
                    exit(SOCKET_ERR);
                }
                std::cout << "creat socket success: " << sock_ << std::endl;

                //2.给服务器指明IP地址和端口port
                struct sockaddr_in local;
                bzero(&local, sizeof(local));

                local.sin_family = AF_INET;
                local.sin_port = htons(port_);
                local.sin_addr.s_addr = INADDR_ANY;
                //local.sin_addr.s_addr = inet_addr(ip_.c_str());//inet_addr函数
                //2.1将字符串IP转化为4字节int
                //2.2将主机序列转化为网络序列

                //3.bind
                if(bind(sock_, (struct sockaddr*)&local, sizeof(local)) < 0){
                    std::cerr << "bind socket error: " << strerror(errno) << std::endl;
                    exit(BIND_ERR);
                }
                std::cout << "bind socket success: " << sock_ << std::endl;
            }

            void Recv(){
                char buffer[2048];
                while(true){
                    //收
                    struct sockaddr_in peer;
                    socklen_t len = sizeof(peer);
                    int n = recvfrom(sock_, buffer, sizeof(buffer)-1, 0, (struct sockaddr*)&peer, &len);
                    if(n > 0)
                        buffer[n] = '\0';
                    else
                        continue;

                    //提取client信息
                    std::string clientip = inet_ntoa(peer.sin_addr);
                    uint16_t clientport = ntohs(peer.sin_port);
                    std::cout << clientip << " - " << clientport << "# " << buffer << std::endl;

                    //做业务
                    std::string message = serverce_(buffer);
                    //发
                    //sendto(sock_, buffer, strlen(buffer), 0, (struct sockaddr*)&peer, sizeof(peer));
                    sendto(sock_, message.c_str(), message.size(), 0, (struct sockaddr*)&peer, sizeof(peer));
                }
            }
            void Start(){

            }

            ~UdpServer(){

            }
        private:
            int sock_;//网络文件描述符
            uint16_t port_;//端口号
            //std::string ip_;//ip号
            func_t serverce_;
    };
}